
var express = require('express')
var router = express.Router(),
    rp = require('request-promise'),
    debug = require('debug')('react-demo')

var configFile = 'config.'+process.env.NODE_ENV+'.json'
var config = require('../../config/'+configFile);

router.post("/", function(req, res, next){
  var token = req.cookies.access_token;
  var param = req.body;
  param.appId = req.cookies.appId

  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/suggestions/queryByView',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }
  debug(option)

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})


router.post("/queryReply", function(req, res, next){
  var token = req.cookies.access_token;
  var param = req.body;
  param.appId = req.cookies.appId

  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/suggestions/queryReply',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }
  debug(option)

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

router.post("/reply", function(req, res, next){
  var token = req.cookies.access_token;
  var param = req.body;
  param.appId = req.cookies.appId

  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/suggestions/reply',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }
  debug(option)

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})

router.post("/updateReply", function(req, res, next){
  var token = req.cookies.access_token;
  var param = req.body;
  param.appId = req.cookies.appId

  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/suggestions/updateReply',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }
  debug(option)

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})


router.post("/queryUnreplyCount", function(req, res, next){
  var token = req.cookies.access_token;
  var param = req.body;
  param.appId = req.cookies.appId

  var option = {
    method: 'POST',
    uri: config.apiHost+'/api/suggestions/queryUnreplyCount',
    body: param,
    headers: {
      authorization: 'Bearer '+token
    },
    json: true
  }
  debug(option)

  rp(option)
    .then(function(result) {
      debug(result);
      // var data = JSON.parse(result)
      if (result.success) {
        return res.json(result)
      }else {
        return res.status(500).json({
          success: false,
          errMsg: result.errMsg
        })
      }
    })
    .catch(function(err) {
      console.error(err);
      return res.status(err.statusCode).json({
        success: false,
        errCode: err.statusCode,
        errMsg: err.error?err.error.errMsg:err
      })
    })
})


module.exports = router
